<?php


namespace app\backend\controller;


use app\base\controller\AdminBaseController;
use think\Exception;
use think\facade\Request;

class Banner extends AdminBaseController
{
    public function index(){
        $list = \app\common\model\Banner::where(['is_deleted'=>0])
            ->order("id","desc")->all();

        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add(){
        return $this->fetch();
    }
    public function edit(){
        $id = Request::param('id',0);
        if(empty($id)|| !is_numeric($id+0)){throw new Exception('数据错误');}
        $bannerInfo = \app\common\model\Banner::where('id',$id)->find()->toArray();
        if(!$bannerInfo){throw new Exception('数据错误');}
        $this->assign('bannerInfo',$bannerInfo);
        return $this->fetch();
    }

    public function deal(){
        $request = Request::post();

        if(empty($request['banner_img'])){wapAjaxReturn(400,'请上传banner图片');}
        if(empty($request['topic_id']) || !is_numeric($request['topic_id'])){wapAjaxReturn(400,'请填写测评id');}

        $banner = new \app\common\model\Banner();
        if(!empty($request['id'])){
            $save = $banner->allowField(true)->save($request,['id'=>$request['id']]);
            $save ? wapAjaxReturn(0,'更新成功',[],'/admin/topic') : wapAjaxReturn(400,'更新失败');

        }else{
            $save = $banner->allowField(true)->save($request);
            $save ? wapAjaxReturn(0,'新增成功',[],'/admin/banner') : wapAjaxReturn(400,'新增失败');

        }

    }
    public function del(){
        $id = Request::param('id',0);
        if(empty($id)|| !is_numeric($id+0)){wapAjaxReturn(400);}

        $topicInfo = \app\common\model\Banner::where('id',$id)->find();
        if(!$topicInfo){wapAjaxReturn(400);}
        $topicInfo->is_deleted = 1;
        $topicInfo->save();
        wapAjaxReturn(0);
    }


}